package main
import (
	"fmt"
	"sync"
	"time"
)
//同时输入
//同时输出
var wg sync.WaitGroup
func putNum(num int,inChann chan int){
	defer wg.Done()
	for i:=2;i<=num;i++{
		inChann<-i
	}
	close(inChann)
}
func primeNum(inChann chan int,primeChann chan int,exitChann chan bool){
	defer wg.Done()
	for num:=range inChann{
		flag:=true
		for i:=2;i<num;i++{
			if num%i==0{
				flag=false
				break
			}
		}
		if flag==true{
			primeChann<-num
		}
	}
	exitChann<-true
}
func printNum(primeChann chan int){
	defer wg.Done()
	// for val:=range primeChann{
	// 	//fmt.Println(val)
	// }
}
func main(){
	start:=time.Now().Unix()
	inchann:=make(chan int,1000)
	primeChann:=make(chan int,1200000)
	exitChann:=make(chan bool,12)
	wg.Add(1)
	go putNum(120000,inchann)
	for i:=1;i<=12;i++{
		wg.Add(1)
		go primeNum(inchann,primeChann,exitChann)
	}
	wg.Add(1)
	go func(){
		for i:=1;i<=12;i++{
			<-exitChann
		}
		close(primeChann)
		wg.Done()
	}()
	wg.Add(1)
	go printNum(primeChann)
	wg.Wait()
	end:=time.Now().Unix()
	fmt.Printf("gap:%d\n",end-start)
	fmt.Println("程序结束")
}